//
//  STag.m
//  STag
//
//  Created by Shea on 2018/7/18.
//  Copyright © 2018年 None. All rights reserved.
//

#import "STag.h"
#import "STagButton.h"
@interface STag()
@property (nonatomic, strong)NSMutableArray *btnArr;
@property (nonatomic, assign)CGFloat totalHeight;
@end

@implementation STag

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setDefaultConfig];
    }
    return self;
}

- (void)setDefaultConfig {
    self.backgroundColor = [UIColor whiteColor];
    self.selectColor = [UIColor whiteColor];
    self.cornerRadius = 3;
    self.lineColor = [UIColor redColor];
    self.lineSelectColor = [UIColor blueColor];
    self.textColor = [UIColor redColor];
    self.textSelectColor = [UIColor blueColor];
    self.lineWidth = 0.5;
    self.fontSize = 12;
    self.itemHegiht = 20;
    self.itemWidth = 50;
    self.topMangin = 8;
    self.leftMangin = 8;
}

- (void)loadData{
    [self.btnArr removeAllObjects];
    CGFloat currentX = self.leftMangin;
    CGFloat currentY = self.topMangin;
    CGFloat maxWidth = self.frame.size.width;
    for (int i = 0; i < self.tagArr.count; i ++) {
        STagButton *button = [STagButton buttonWithType:UIButtonTypeCustom];
        CGFloat titleWidth = [self caculateStrSize:self.tagArr[i] font:self.fontSize];
        if (titleWidth > maxWidth || titleWidth + currentX + self.leftMangin > maxWidth) {
            if (titleWidth > maxWidth) {
                titleWidth = self.frame.size.width - 2 * self.leftMangin;
            }
            if(!(currentY == self.topMangin && currentX == self.leftMangin)) {
                currentY += self.topMangin + self.itemHegiht;
            }
            currentX = self.leftMangin;
            button.frame = CGRectMake(currentX, currentY, titleWidth, self.itemHegiht);
            currentX = maxWidth;
        }else {
            button.frame = CGRectMake(currentX, currentY, titleWidth, self.itemHegiht);
            currentX += titleWidth + self.leftMangin;
        }
        
        button.titleLabel.font = [UIFont systemFontOfSize:self.fontSize];
        [button setTitle:self.tagArr[i] forState:UIControlStateNormal];
        [button setTitleColor:self.textColor forState:UIControlStateNormal];
        [button setTitleColor:self.textSelectColor forState:UIControlStateSelected];
        [button setBackgroundColor:self.backgroundColor];
        button.layer.cornerRadius = self.cornerRadius;
        button.layer.borderWidth = self.lineWidth;
        button.layer.borderColor = self.lineColor.CGColor;
        button.layer.masksToBounds = YES;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.buttonModel = self.tagArr[i];
        
        [self.btnArr addObject:button];
        [self addSubview:button];
    }
    self.totalHeight = currentY + self.itemHegiht + self.topMangin;
}

- (void)buttonClick:(STagButton *)sender {
    sender.selected = !sender.selected;
    [self buttonSelectChange:sender];
    if (!self.isMoreSelect) {
        if(sender.selected) {
            if (self.moreSelectArr.count) {
                STagButton *button = self.moreSelectArr[0];
                button.selected = !button.selected;
                [self buttonSelectChange:button];
                [self.moreSelectArr removeObject:button];
            }
            [self.moreSelectArr addObject:sender];
            
        }else {
            [self.moreSelectArr removeAllObjects];
        }
    }else {
        if(sender.selected) {
            [self.moreSelectArr addObject:sender];

        }else {
            [self.moreSelectArr removeObject:sender];
        }
    }
    NSLog(@"count:%ld\ncontent:%@",self.moreSelectArr.count,self.moreSelectArr);
}

- (void)buttonSelectChange:(STagButton *)button {
    if (button.selected) {
        [button setBackgroundColor:self.selectColor];
        button.layer.borderColor = self.lineSelectColor.CGColor;
    }else {
        [button setBackgroundColor:self.backgroundColor];
        button.layer.borderColor = self.lineColor.CGColor;
    }
}


- (CGFloat)caculateStrSize:(NSString *)str font:(CGFloat)fontSize {
    CGSize size = [str sizeWithAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:fontSize]}];
    if (size.width + 20 < self.itemWidth) {
        return self.itemWidth;
    }
    return size.width + 20;
}

- (CGFloat)getViewTotalHeight {
    return self.totalHeight;
}

- (NSArray *)tagArr {
    if (!_tagArr) {
        _tagArr = [[NSArray alloc] init];
    }
    return _tagArr;
}

- (NSMutableArray *)moreSelectArr {
    if (!_moreSelectArr) {
        _moreSelectArr = [[NSMutableArray alloc] init];
        
    }
    return _moreSelectArr;
}

- (NSMutableArray *)btnArr {
    if (!_btnArr) {
        _btnArr = [[NSMutableArray alloc] init];
    }
    return _btnArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
